package com.jcl.elasticsearch.recruit.service;

import com.alibaba.fastjson.JSONObject;
import com.jcl.core.utils.HttpRequest;
import com.jcl.elasticsearch.core.entity.ResumeDownloadInfo;
import com.jcl.elasticsearch.resume.logic.ResumeDownloadLogic;
import com.jcl.elasticsearch.resume.model.ResumeExternalMsg;
import com.jcl.elasticsearch.server.ResumeApiProxy;
import com.jcl.elasticsearch.thread.ResumeDownloadTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RecruitElasticService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/26 14:36
 * @Version 1.0
 **/
@Service
public class RecruitElasticService {

    @Autowired
    private ResumeDownloadLogic resumeDownloadLogic;

    @Autowired
    private ResumeApiProxy resumeApiProxy;

    /**
     * TODO
     *
     * @return void
     * @Date 13:57 2018/11/20
     **/
    public void completeResume(ResumeDownloadInfo entity) {
        resumeApiProxy.completeResume(entity);
    }

    public ResumeExternalMsg download(String tenantId, String corpCode, Integer resumeId) {

        ResumeExternalMsg resumeExternalMsg = resumeDownloadLogic.getExternalMsg(resumeId);
        if (null == resumeExternalMsg) {
            // 没有外部关联信息
            return null;
        }
        Map<String, Object> data = resumeDownloadLogic.createDownloadMap(tenantId, corpCode, resumeExternalMsg);

        ResumeDownloadTask.start(data);

        return resumeExternalMsg;
    }

    public static void main(String[] args) {
        Map<String, Object> data = new HashMap<>();
        data.put("taskType", "buy-resume");
        String dd = "eyJjVXNlcklkIjogIjY1NTcyMTgiLCAicGFzc3dvcmQiOiAibGMxMjM0NTYiLCAidXJsIjogImh0dHBzOi8vZWFzeS5sYWdvdS5jb20vL3NlYXJjaC9yZXN1bWUvZmV0Y2hSZXN1bWUuaHRtP3Jlc3VtZUZldGNoS2V5PTZZamQwU1AtZnpmanNlOVNyYzF4Uml3QkZrTWJaZTVIckpMaXo3LUVpa1l2elhsRWNlcVlRZ1NoRWtRSUJFMXFTMHFvSTF2RlgtOXFqY1MxdktYVS1RPT0mb3V0ZXJQb3NpdGlvbklkPSIsICJ1c2VybmFtZSI6ICIxODUxNjI2MTI1MCJ9";
        String _externalMsg = ResumeExternalMsg.decode(dd);
        data.put("buyResumeContext", JSONObject.parse(_externalMsg));
        data.put("siteCode", "lagou");
        data.put("corpCode", "JCL0000002");
        data.put("tenantId", "C1CBED6C4E7717BA05CEBFBBC089BEF0");
        String url = "http://192.168.33.250:7777/api/v1.0/get_info";
        String message = HttpRequest.httpPost(url, HttpRequest.JSON_CONTENT_TYPE, data);

        System.out.println(message);
    }
}
